package com.stu.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.common.R;
import com.stu.pojo.*;
import com.stu.pojo.vo.StudentDescVo;
import com.stu.pojo.vo.StudentVo1;
import com.stu.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    IStudentService studentService;
    @Autowired
    IStudentDescService studentDescService;
    @Autowired
    IAccountFlowService accountFlowService;
    @Autowired
    IAccountService accountService;

    @GetMapping("/output")
    public void output(@RequestParam Set<String> includeColumnFiledNames,
                       String name,String clazzName,String study,String school,String payStatus,
                       HttpServletResponse response){
        System.out.println(includeColumnFiledNames);
        response.setHeader("Content-Disposition","attachment;filename="+UUID.randomUUID()+"stuoa.xlsx");
        List<StudentDescVo> studentDescVos = studentService.getStudentAndDesc(name,clazzName,study,school,payStatus);
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        EasyExcel.write(out, StudentDescVo.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
                .doWrite(studentDescVos);
        System.out.println(studentDescVos);
    }
    @GetMapping("/page")
    public R page1(Integer page,Integer pageSize,
                   String name,String clazzName,String study,String school,String payStatus){
        Page<StudentDescVo> studentDescVoPage = studentService.lendingList(page, pageSize,name,clazzName,study,school,payStatus);
        return R.success(studentDescVoPage);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        StudentDescVo studentDescVo = studentService.getByIdWithDesc(id);
        if (studentDescVo != null){
            return R.success(studentDescVo);
        }
        return R.error("没有查询到对应学生信息");
    }

    @PutMapping
    public R update(@RequestBody StudentDescVo studentDescVo){
        boolean flag = studentService.updateByIdWithDesc(studentDescVo);
        return flag ? R.success("修改成功"):R.error("修改失败");
    }

    //退款
    @Transactional
    @PostMapping("/returnMoney")
    public R returnMoney(@RequestBody Map<String,String> map){
        log.info("map:{}",map);
        R r = accountService.returnMoney(map);
        return r;
    }
    @Autowired
    private StudentTuiService studentTuiService;
    //退款
    @Transactional
    @DeleteMapping("/delete")
    public R returnMoney(@RequestBody StudentDescVo studentDescVo){
        log.info("删除studentDescVo:{}",studentDescVo);
        R r= studentTuiService.tuixue(studentDescVo);
        return r;
    }

}

